package com.foreverwin.jzymes.websocket;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.TranshipInfra;
import com.foreverwin.jzymes.common.mapper.TranshipInfraAdviceMapper;
import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.framework.annotation.WebSocketHandler;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import com.foreverwin.jzymes.framework.websocket.TextMessageWebSocketWraper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@WebSocketHandler(channel = "Z_DB_MAIN_LINE")
public class MainLineHandler extends TextMessageWebSocketWraper {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;
    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    WebSocketService webSocketService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    TranshipInfraAdviceMapper transhipInfraAdviceMapper;

    private Map<String,String> activityRuleMap;

    public MainLineHandler(){
        events = new ArrayList();
        events.add( "RESOURCE_STATUS_CHANGE" );
        events.add( "AGV_DISPATCH" );
        events.add( "TRANSHIP_TRANSFER_EVENT" );
    }

    @Override
    public void doAfterOpen(Connection connection) {
        activityRuleMap = activityRuleAdviceService.findActivityRules( getChannel() );
    }

    @Override
    public void doAfterClose() {

    }

    @Override
    public void onAppEvent(AppEvent appEvent) {
        String event = appEvent.getName();
        JSONObject eventData = (JSONObject) appEvent.getData();
        if( "RESOURCE_STATUS_CHANGE".equals( event ) ){
            String site = eventData.getString( "SITE" );
            String resrce = eventData.getString( "RESRCE" );
            String status = eventData.getString( "STATUS" );

            JSONObject response = new JSONObject();
            response.put( "id", resrce );
            response.put( "status", status );
            sendMessage( "WS_RESOURCE_STATUS", response );
        }

        if( "AGV_DISPATCH".equals( event ) ){
            String channel = eventData.getString( "CHANNEL" );
            if( channel.equals( getChannel() ) ){
                String messageType = eventData.getString( "MESSAGE_TYPE" );
                JSONObject value = eventData.getJSONObject( "VALUE" );
                sendMessage( messageType, value );
            }
        }

        if( "TRANSHIP_TRANSFER_EVENT".equals( event ) ){

            Map<String,Object> transhipMap = new HashMap<>();
            transhipMap.put( "id", eventData.get( "RESRCE" ) );
            transhipMap.put( "rawNum", eventData.get( "RAW_NUM" ) );
            transhipMap.put( "clinkerNum", eventData.get( "CLINKER_NUM" ) );
            transhipMap.put( "status", resourceAdviceService.findResourceStatus( eventData.getString( "SITE" ), eventData.getString( "RESRCE" ) ) );

            List<Map<String,Object>> ports = new ArrayList<>();
            ports.add( transhipMap );

            JSONObject valueMap = new JSONObject();
            valueMap.put( "ports", ports );
            sendMessage( "WS_MASTER_TRANSHIP_INFRA", valueMap );
        }
    }

    @Override
    public JSONObject doReceiveAndReply( String messageType, JSONObject requestParams ) {
        JSONObject responseBody = null;
        String channel = getChannel();

        if( "WS_INIT_DATA".equals( messageType ) ){
            String site = activityRuleMap.get( "SITE" );
            responseBody = findTranshipListByDateRange( site );

            JSONObject agvData = webSocketService.findPageData( site, channel, "AGV_INIT_DATA" );
            if( agvData != null ){
                responseBody.put( "agvs", agvData.getJSONArray( "agvs" ) );
            }
        }

        return responseBody;
    }

    public JSONObject findTranshipListByDateRange( String site ){

        Date[] dateRange = productionShiftService.findShiftRange( site, new Date() );
        List<TranshipInfra>  transhipInfraList = transhipInfraAdviceMapper.selectTranshipInfraByDateRange( site, dateRange[0], dateRange[1] );

        List<Map<String,Object>> ports = new ArrayList<>();
        for( TranshipInfra transhipInfra : transhipInfraList ){
            Map<String,Object> transhipMap = new HashMap<>();
            transhipMap.put( "id", transhipInfra.getResrce() );
            transhipMap.put( "rawNum", transhipInfra.getRawQty() );
            transhipMap.put( "clinkerNum", transhipInfra.getProductQty() );
            transhipMap.put( "status", resourceAdviceService.findResourceStatus( site, transhipInfra.getResrce() ) );
            ports.add( transhipMap );
        }

        JSONObject responseBody = new JSONObject();
        responseBody.put( "date", DateUtil.format( new Date(), "yyyy-MM-dd" ) );
        responseBody.put( "time", DateUtil.format( new Date(), "HH:mm" ) );
        responseBody.put( "ports", ports );

        return responseBody;
    }

}
